package com.yunmang.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.bigkoo.alertview.AlertView;
import com.bigkoo.alertview.OnItemClickListener;
import com.yunmang.R;
import com.yunmang.app.AppConstant;
import com.yunmang.entity.ParameterEntity.NextStationBean;
import com.yunmang.http.HttpUtils;
import com.yunmang.utils.CommonUtils;
import com.yunmang.utils.SharedPreferencesUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import rx.Subscriber;

/**
 * Created by Administrator on 2017/5/8.
 */
public class TransportActivity extends BaseActivity implements OnItemClickListener {

    @InjectView(R.id.toolbar)
    Toolbar mToolbar;
    @InjectView(R.id.next_station)
    TextView nextStation;
    @InjectView(R.id.start_station)
    TextView startStation;
    @InjectView(R.id.next)
    Button btn_next;
    @InjectView(R.id.et_card_number)
    EditText etCardNumber;
    @InjectView(R.id.et_driver_name)
    EditText etDriverName;
    @InjectView(R.id.et_driver_phone)
    EditText etDriverPhone;
    private String nextID;
    private Subscriber subscriber;
    private List<NextStationBean.ModelBean> modelList;
    private AlertView nextAlertView;
    private String car_num,drivername,drvierphone;
    @Override
    public int getLayoutId() {
        return R.layout.activity_transport;
    }

    @Override
    public void initView() {
        ButterKnife.inject(this);
        initToolbar();
    }

    private void initToolbar() {
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("干线运输");
        mToolbar.setNavigationIcon(R.mipmap.ic_back);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }


    @Override
    public void initData() {
        startStation.setText(AppConstant.orgInfo.getName());

        String phone=SharedPreferencesUtils.getParameter(this, "sjdh");
        String chepai=SharedPreferencesUtils.getParameter(this, "plate");
        String name=SharedPreferencesUtils.getParameter(this, "sjxm");
        if(!TextUtils.isEmpty(phone)&&!TextUtils.isEmpty(name)){
            etCardNumber.setText(chepai);
            etDriverPhone.setText(phone);
            etDriverName.setText(name);
        }
    }


    @OnClick({R.id.next_station, R.id.next})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.next_station:
                getNextStation();
                break;
            case R.id.next:
                car_num= CommonUtils.getTextViewValue(etCardNumber);
                drivername=CommonUtils.getTextViewValue(etDriverName);
                drvierphone=CommonUtils.getTextViewValue(etDriverPhone);
                if (TextUtils.isEmpty(nextID)) {
                    showToast("请选择下一站");
                    return;
                }
                if(TextUtils.isEmpty(drivername)){
                    showToast("请输入司机姓名");
                    return;
                }
                if(TextUtils.isEmpty(drvierphone)){
                    showToast("请输入司机手机号");
                    return;
                }
                if(TextUtils.isEmpty(car_num)){
                    showToast("请输入车牌号");
                    return;
                }
                Intent in = new Intent(context, TransportDetailActivity.class);
                in.putExtra("nextId", nextID);
                in.putExtra("drivername",drivername);
                in.putExtra("drvierphone",drvierphone);
                in.putExtra("car_num",car_num);
                in.putExtra("nextstation",CommonUtils.getTextViewValue(nextStation));
                saveData(drivername,drvierphone,car_num);
                startActivity(in);
                break;
        }
    }

    /**
     * 保存上次填写的数据
     * @param drivername
     * @param drvierphone
     * @param car_num
     */
    private void saveData(String drivername, String drvierphone, String car_num) {
        SharedPreferencesUtils.saveParameter(this, "sjxm",drivername);
        SharedPreferencesUtils.saveParameter(this, "sjdh",drvierphone);
        SharedPreferencesUtils.saveParameter(this, "plate",car_num);
    }

    private void getNextStation() {
        showWaitingDialog("获取数据中……");
        subscriber = new Subscriber<NextStationBean>() {
            @Override
            public void onCompleted() {
                hideWaitingDialog();
            }

            @Override
            public void onError(Throwable e) {
                hideWaitingDialog();
            }

            @Override
            public void onNext(NextStationBean nextStationBean) {
                hideWaitingDialog();
                if (nextStationBean.getCode().equals("200")) {
                    modelList = nextStationBean.getModel();
                    fillNextStation(modelList);
                }
            }
        };
        HttpUtils.getInstance().getNextStation(subscriber, AppConstant.orgInfo.getId() + "");
    }

    private void fillNextStation(List<NextStationBean.ModelBean> modelList) {
        List<String> nextArray = new ArrayList<>();
        for (NextStationBean.ModelBean model : modelList) {
            nextArray.add(model.getName());
        }
        String[] array = new String[nextArray.size()];
        nextAlertView = new AlertView(null, null, null, null, nextArray.toArray(array), this, AlertView.Style.Alert, this);
        nextAlertView.show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onItemClick(Object o, int position) {
        if (o == nextAlertView && position != AlertView.CANCELPOSITION) {
            nextStation.setText(modelList.get(position).getName());
            nextID = modelList.get(position).getId();
        }
    }

}
